package com.xunk.scene.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xunk.common.core.utils.BeanUtil;
import com.xunk.common.core.utils.WeatherUtil;
import com.xunk.common.core.web.domain.AjaxResult;
import com.xunk.common.redis.service.RedisService;
import com.xunk.scene.domain.SceneConditionWeather;
import com.xunk.scene.domain.Weather;
import com.xunk.scene.mapper.SceneConditionWeatherMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @ProjectName: HIM
 * @Package: com.xunk.scene.service
 * @ClassName: WeatherService
 * @Author: tangguangyuan
 * @Description: 天气service类
 * @Date: 2021/5/18 15:13
 * @Version: 1.0
 */
@Service
public class WeatherService {
    @Autowired
    private RedisService redisService;
    @Autowired
    private SceneConditionWeatherMapper sceneConditionWeatherMapper;

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 根据地区code查询天气
     *
     * @param addressCode
     * @return
     */
    public Weather queryWeatherFromAmap(Long addressCode) {
        Weather weather = null;
        weather = redisService.getCacheObject("weather:" + addressCode);

        if (BeanUtil.isEmptyBean(weather)) {
            //调用高德API获取天气
            JSONObject response = WeatherUtil.queryWeatherFromAmap(addressCode);


            weather = JSON.toJavaObject(response, Weather.class);
            String redisKey = "weather:" + weather.getAdcode();
            //将该地区天气存入缓存设置超时时间3600s
            redisService.setCacheObject(redisKey, weather, 3600, TimeUnit.SECONDS);
            log.info(String.format("地区:%d天气缓存成功", addressCode));
            System.out.println(weather);
        }
        return weather;
    }



    /**
     * 异步缓存天气
     *
     * @param addressCode
     * @return
     */
    @Async
    public void asyncQueryWeather(Long addressCode) {
        Weather weather = null;
        weather = redisService.getCacheObject("weather:" + addressCode);

        if (BeanUtil.isEmptyBean(weather)) {
            //调用高德API获取天气
            JSONObject response = WeatherUtil.queryWeatherFromAmap(addressCode);
            weather = JSON.toJavaObject(response, Weather.class);
            String redisKey = "weather:" + weather.getAdcode();
            //将该地区天气存入缓存设置超时时间3600s
            redisService.setCacheObject(redisKey, weather, 3600, TimeUnit.SECONDS);
            log.info(String.format("地区:%d天气缓存成功", addressCode));
            System.out.println(weather);
        }
    }

    /**
     * 将天气条件表中包含的地区天气进行缓存
     *
     * @return
     */
    public AjaxResult cacheWeather() {
        log.info("-----------缓存高德天气数据开始------------");
        List<SceneConditionWeather> sceneConditionWeatherList = sceneConditionWeatherMapper.queryWeatherConditionEffective();
        List<Long> adCodeList = sceneConditionWeatherList.stream().map(SceneConditionWeather::getAdCode).collect(Collectors.toList());
        for (Long adCode :
                adCodeList) {
            asyncQueryWeather(adCode);
        }
        log.info("-----------缓存高德天气数据结束------------");

        return AjaxResult.success("缓存天气数据成功");
    }
}
